package cn.edu.neusoft.amicooldemo.model;

import java.util.List;

import cn.edu.neusoft.amicooldemo.bean.ArticleBean;
import cn.edu.neusoft.amicooldemo.bean.CollectResult;
import cn.edu.neusoft.amicooldemo.bean.ProjectBean;
import cn.edu.neusoft.amicooldemo.bean.TcaseBean;
import cn.edu.neusoft.amicooldemo.bean.TwareBean;
import cn.edu.neusoft.amicooldemo.bean.VideoBean;
import cn.edu.neusoft.amicooldemo.iface.CollectIface;
import cn.edu.neusoft.amicooldemo.iface.CollectListListener;
import cn.edu.neusoft.amicooldemo.iface.CollectListener;
import cn.edu.neusoft.amicooldemo.service.CollectService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Neusoft on 2016/12/21.
 */

public class CollectModel extends BaseModel implements CollectIface{
    private CollectService service;
    private static CollectModel model;


    public CollectModel(){
        super();
    }
    public static CollectModel getInstance(){
        if(model==null){
            model = new CollectModel();

        }
        return model;
    }

    @Override
    public void collect(String mod, int resid, String sessionID, final CollectListener listener) {
        service = createService(CollectService.class,"scalar");
        Call<String> call = service.collect(mod,resid,sessionID);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.body()==null)
                listener.onFail("0");
                else if(response.body().trim().toString().equals("0"))
                    listener.onResponse("1");
                else
                    listener.onResponse("3");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail("0");
            }
        });
    }

    @Override
    public void uncollect(String mod, int resid, String sessionID, final CollectListener listener) {
        service = createService(CollectService.class,"scalar");
        Call<String> call = service.uncollect(mod,resid,sessionID);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.body()==null)
                    listener.onFail("0");
                else if(response.body().trim().toString().equals("0"))
                    listener.onResponse("3");
                else
                    listener.onResponse("1");

            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail("fail:"+t.toString());
            }
        });
    }

    @Override
    public void exists(String mod, int resid, String sessionID, final CollectListener listener) {
        service = createService(CollectService.class,"scalar");
        Call<String> call = service.exists(mod,resid,sessionID);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.body()==null)
                    listener.onFail("0");
                else if(response.body().trim().toString().equals("0"))
                    //已收藏
                    listener.onResponse("3");
                else
                    listener.onResponse("1");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail("fail:"+t.toString());
            }
        });
    }

    @Override
    public void getResultList(String mod, int page, String sessionID, CollectListListener listener) {
        service = createService(CollectService.class);
        switch (mod) {
            case "video":
                Call<List<CollectResult<VideoBean>>> call = service.getCVideoList(mod, page, sessionID);
                call.enqueue(new CallbackCollect<VideoBean>(page, listener));
                break;
            case "article":
                Call<List<CollectResult<ArticleBean>>> call2 = service.getCArticleList(mod, page, sessionID);
                call2.enqueue(new CallbackCollect<ArticleBean>(page, listener));
                break;
            case "tware":
                Call<List<CollectResult<TwareBean>>> call3 = service.getCTwareList(mod, page, sessionID);
                call3.enqueue(new CallbackCollect<TwareBean>(page, listener));
                break;
            case "tcase":
                Call<List<CollectResult<TcaseBean>>> call4 = service.getCTcaseList(mod, page, sessionID);
                call4.enqueue(new CallbackCollect<TcaseBean>(page, listener));
                break;
            case "project":
                Call<List<CollectResult<ProjectBean>>> call5 = service.getCProjectList(mod, page, sessionID);
                call5.enqueue(new CallbackCollect<ProjectBean>(page, listener));
                break;
        }

    }

    @Override
    public void getResultBean(String mod, int id, String sessionID, CollectListListener listener) {

    }
}
